package com.yt.order.api.client;

import com.yt.common.constant.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yt.order.models.service.IStatisticsService;
import com.yt.order.models.vo.StatisticsVO;

/**
 * 统计接口控制器
 *
 * @author xieyanfei
 * @Date 2025-09-05 15:45:00
 */
@RestController
@RequestMapping("/api/statistics")
@Slf4j
@Api(value = "StatisticsApiController", tags = "统计接口")
public class StatisticsApiController {

    @DubboReference
    private IStatisticsService statisticsService;

    @PostMapping("/homePage")
    @ApiOperation(value = "获取首页统计数据", notes = "获取工单总数、今日新增工单和客户总数")
    public Result<StatisticsVO> getHomePageStatistics() {
        try {
            StatisticsVO statistics = statisticsService.getHomePageStatistics();
            return new Result<>(statistics);
        } catch (Exception e) {
            log.error("获取首页统计数据失败", e);
            return new Result<>(com.yt.common.enums.CodeEnum.FAIL_100001);
        }
    }
    // 新增：工单总数接口
    @PostMapping("/totalWorkOrders")
    @ApiOperation(value = "获取工单总数", notes = "统计系统中所有工单的总数")
    public Result<Integer> getTotalWorkOrders() {
        try {
            int count = statisticsService.getTotalWorkOrders();
            return new Result<>(count);
        } catch (Exception e) {
            log.error("获取工单总数失败", e);
            return new Result<>(com.yt.common.enums.CodeEnum.FAIL_100001);
        }
    }

    // 新增：今日新增工单接口
    @PostMapping("/todayNewWorkOrders")
    @ApiOperation(value = "获取今日新增工单数量", notes = "统计当天新增的工单数量")
    public Result<Integer> getTodayNewWorkOrders() {
        try {
            int count = statisticsService.getTodayNewWorkOrders();
            return new Result<>(count);
        } catch (Exception e) {
            log.error("获取今日新增工单失败", e);
            return new Result<>(com.yt.common.enums.CodeEnum.FAIL_100001);
        }
    }

    // 新增：客户总数接口
    @PostMapping("/totalCustomers")
    @ApiOperation(value = "获取客户总数", notes = "统计系统中所有有效客户的总数")
    public Result<Integer> getTotalCustomers() {
        try {
            int count = statisticsService.getTotalCustomers();
            return new Result<>(count);
        } catch (Exception e) {
            log.error("获取客户总数失败", e);
            return new Result<>(com.yt.common.enums.CodeEnum.FAIL_100001);
        }
    }
}